package com.appspot.theonionorthenews;

import javax.persistence.Entity;
import javax.persistence.Id;

import com.google.appengine.api.memcache.MemcacheService;

/**
 * This persistent class exists mostly to group {@link NewsItemEntity} objects into
 * an entity group.  By doing so, we get better locality and therefore better
 * performance out of the data store, and we avoid needing the cross-group
 * transaction option.
 *
 * As long as we need a persistent parent for the group, it's convenient to put
 * the maximal draw ID into that parent, which avoids race conditions if using
 * {@link MemcacheService#increment(Object, long)} as a unique ID generator, in
 * cases where the cache entry does not exist (or has been evicted).
 */
@Entity
public class NewsGroupEntity {

  /** A human-readable name for the group, unique as a primary key. */
  @Id
  private String name;

  /** For this group, what is the highest assigned draw id? */
  private long maxDraw;

  /** Creates a new group (without any draw ids defined). */
  public NewsGroupEntity(String name) {
    this.name = name;
    maxDraw = -1;
  }

  /** JPA-required default constructor. */
  NewsGroupEntity() {
  }

  public long getMaxDraw() {
    return maxDraw;
  }

  public void setMaxDraw(long newMax) {
    maxDraw = newMax;
  }

  public String getName() {
    return name;
  }
}
